package pri.ydy.eduService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import pri.ydy.eduService.entity.EduChapter;
import pri.ydy.eduService.entity.EduVideo;
import pri.ydy.eduService.entity.chapter.ChapterVo;
import pri.ydy.eduService.entity.chapter.VideoVo;
import pri.ydy.eduService.mapper.EduChapterMapper;
import pri.ydy.eduService.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import pri.ydy.eduService.service.EduVideoService;
import pri.ydy.serviceBase.exceptionhandler.GuliException;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-10-06
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    //注入videoService
    @Autowired
    private EduVideoService videoService;
    //根据课程id进行查询，课程大纲列表
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        //1.根据课程id查询课程所有的章节
        //条件查询用wrapper
        QueryWrapper<EduChapter> wrapperChapter=new QueryWrapper<>();
        wrapperChapter.eq("course_id",courseId);
        List<EduChapter> chapterList = baseMapper.selectList(wrapperChapter);

        //2.根据课程id查询课程里面所有的小节
        QueryWrapper<EduVideo> wrapperVideo=new QueryWrapper<>();
        wrapperVideo.eq("course_id",courseId);
        List<EduVideo> videoList = videoService.list(wrapperVideo);

        //创建最终list集合
        List<ChapterVo> finalList=new ArrayList<>();
        //3.遍历查询章节list集合进行封装
        for (int i = 0; i < chapterList.size(); i++) {
            //获取每一个章节对象
            EduChapter chapter = chapterList.get(i);
            ChapterVo chapterVo=new ChapterVo();
            BeanUtils.copyProperties(chapter,chapterVo);

            //封装二级对象
            List<VideoVo> videoVoList=new ArrayList<>();
            for (int j = 0; j < videoList.size(); j++) {
                EduVideo eduVideo = videoList.get(j);
                //判断小节id是否为章节id
                if (eduVideo.getChapterId().equals(chapterVo.getId())) {
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo, videoVo);
                    //放入节点集合数组
                    videoVoList.add(videoVo);
                }
            }
            //放入子
            chapterVo.setChildren(videoVoList);
            finalList.add(chapterVo);
        }
        return finalList;
    }

    //删除章节，有小节不让删除
    @Override
    public boolean deleteChapter(String chapterId) {
        //根据chapterid查询小节表，无则删除，有则不删
        QueryWrapper<EduVideo> wrapper=new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        int count = videoService.count(wrapper);
        if(count>0){
            throw new GuliException(20001,"存在小节，无法删除");
        }else{
            int i = baseMapper.deleteById(chapterId);
            return i>0;
        }
    }

    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper=new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
